/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.luedinski.objects;

import java.lang.Integer;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jgrapht.GraphPath;
import org.jgrapht.Graphs;

/**
 *
 * @author lueder
 */
public class Trip {
    private List<List<Route>> routes;
    private List<List<Station>> paths;
    private Station origin;
    private Station destination;
    private Time departure;
    
    public Trip(){
        routes = new ArrayList<List<Route>>();
        paths = new ArrayList<List<Station>>();
        departure = new Time();
    }
    
    public void generateTrips(Station origin, Station destination, Time departure, Net net, Map<Integer, Route> routes){
        this.origin = origin;
        this.destination = destination;
        this.departure = departure;
        
        DecimalFormat f = new DecimalFormat("#0.00");
        // Alle möglichen Wege
        Iterator<GraphPath<Station, Station>> paths = net.getRoutes(origin, destination, 4);
        int i = 0;
        while (paths.hasNext()) {
            GraphPath<Station, Station> path = paths.next();

            System.out.println((++i) + ".: " + path.getStartVertex().getName() + " -> " + path.getEndVertex().getName());

            // Geordnete Liste aller Stationen eines Weges
            Iterator<Station> stationIter = Graphs.getPathVertexList(path).iterator();
            Station actual = null;
            Station prev = null;
            double distance = 0;
            int stops = 0;
            ArrayList<Station> actualPath = new ArrayList<Station>();
            HashMap<Station, ArrayList<Route>> hashmap = new HashMap<Station, ArrayList<Route>>();
            // Alle Stations eines Weges ablaufen:
            while (stationIter.hasNext()) {
                stops++;
                
                // Aktuelle Station
                actual = stationIter.next();
                
                // Erst aber der 2. Station berechnen (1. Edge)
                if (prev != null) {
                    actualPath.add(actual);
                    // Gesamtdistanz erhöhen
                    distance += net.getDistance(prev, actual);
                    System.out.println(prev.getName() + " -> " + actual.getName());
                    
                    // Züge finden ohne Zeit!
                    Iterator<Route> routesIter = routes.values().iterator();
                    ArrayList<Route> potentialRoutes = new ArrayList<Route>();
                    
                    while(routesIter.hasNext()){
                        Route route = routesIter.next();
                        if(route.containsEdge(prev, actual)){
                            potentialRoutes.add(route);
                            System.out.println(route.toString());
                            //System.out.println(route.toString());
                            
                        }
                    }
                    hashmap.put(actual, potentialRoutes);
                    
                    // Züge mit Zeiten finden
                    /*ArrayList<ScheduledRoute> possibleTrains = schedule.getTrains(prev, actual, departure);
                    for (int j = 0; j < possibleTrains.size(); j++) {
                        System.out.println(j+": "+possibleTrains.get(j).toString(prev));
                        //System.out.print(("R: "+(routesList.get(j).containsEdge(prev, actual) ? routesList.get(j).getName() + "\n" : "")));
                    }*/
                }
                prev = actual;
            }
            // TODO hashmap berechnen, beste Verbindung nach Zeiten, dann Geschw./ Umsteigen
            // TODO beste verbindung für den Pfad in Klassenvariable einfügen!
            System.out.println("Distance: " + f.format(distance));
            System.out.println("Stops: " + (stops - 2) + "\n");

        }
    }

	public Station getOrigin() {
		return origin;
	}

	public void setOrigin(Station origin) {
		this.origin = origin;
	}

	public Station getDestination() {
		return destination;
	}

	public void setDestination(Station destination) {
		this.destination = destination;
	}

	public Time getDeparture() {
		return departure;
	}

	public void setDeparture(Time departure) {
		this.departure = departure;
	}
	
	public boolean isValidUserInputData(){
		if(origin!=null&&destination!=null&&departure!=null)
			return true;
		else
			return false;
	}
}
